package com.wy.cms.initconfig;

import com.wy.common.exception.BizException;
import com.wy.constant.ConfigConst;
import com.wy.constant.RedisConst;
import com.wy.domain.AreaCity;
import com.wy.domain.AreaCounty;
import com.wy.domain.AreaProvince;
import com.wy.protocol.ERRORCODE;
import com.wy.service.ex.IAreaCityExService;
import com.wy.service.ex.IAreaCountyExService;
import com.wy.service.ex.IAreaProvinceExService;
import com.wy.util.RedisUtil;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class AreaInitConfig {

	private static Element root = null;

	static{
		SAXReader sr = new SAXReader();
		try {
			InputStream in = CmsInitConfig.class.getClassLoader().getResourceAsStream(ConfigConst.AREA);
			Document doc = sr.read(in);
			root = doc.getRootElement();
			in.close();
			doc.clearContent();
		} catch (Exception e) {
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
	}

	@Autowired
	private IAreaProvinceExService areaProvinceExService;

	@Autowired
	private IAreaCityExService areaCityExService;

	@Autowired
	private IAreaCountyExService areaCountyExService;


	private void initArea() throws Exception{
		areaCountyExService.deleteAll();
		areaCityExService.deleteAll();
		areaProvinceExService.deleteAll();
		AreaProvince provincevo = new AreaProvince();
		AreaCity cityvo = new AreaCity();
		AreaCounty countyvo = new AreaCounty();
		List<Element> provinces = root.elements("province");
		if(null!=provinces&&provinces.size()>=0){
			List<AreaProvince> areaProvinces = new ArrayList<AreaProvince>();
			List<AreaCity> areaCitys = new ArrayList<AreaCity>();
			List<AreaCounty> areaCountys = new ArrayList<AreaCounty>();
			for(Element province:provinces){
				provincevo = new AreaProvince();
				provincevo.setId(province.attributeValue("id"));
				provincevo.setName(province.attributeValue("name"));
				areaProvinces.add(provincevo);
				List<Element> citys = province.elements("city");
				if(null!=citys&&citys.size()>=0){
					for(Element city:citys){
						cityvo = new AreaCity();
						cityvo.setId(city.attributeValue("id"));
						cityvo.setName(city.attributeValue("name"));
						cityvo.setProvinceId(Long.valueOf(province.attributeValue("id")));
						areaCitys.add(cityvo);
						List<Element> countys = city.elements("county");
						if(null!=countys&&countys.size()>=0){
							for(Element county:countys){
								countyvo = new AreaCounty();
								countyvo.setId(county.attributeValue("id"));
								countyvo.setName(county.attributeValue("name"));
								countyvo.setCityId(Long.valueOf(city.attributeValue("id")));
								areaCountys.add(countyvo);
							}
							countys.clear();
						}
					}
					citys.clear();
				}
			}
			provinces.clear();
			areaProvinceExService.batchInsert(areaProvinces);
			areaProvinces.clear();
			areaCityExService.batchInsert(areaCitys);
			areaCitys.clear();
			areaCountyExService.batchInsert(areaCountys);
			areaCountys.clear();
		}
	}

	public static void cleanArea(){
		RedisUtil.getInstance().del(RedisConst.AREA_PROVINCE);
	}
	
}
